package com.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.AjaxLoader;
import com.google.gwt.visualization.client.visualizations.AreaChart;
import com.google.gwt.visualization.client.visualizations.BarChart;
import com.google.gwt.visualization.client.visualizations.ColumnChart;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.google.gwt.visualization.client.visualizations.PieChart;
import com.google.gwt.visualization.client.visualizations.Table;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CNStatsFront implements EntryPoint {

	private static CNStatsFront singleton;

	public CNStatsFrontConstants constants;

	public VerticalPanel plMain;
	public FlexTable ftHeader;
	public DeckPanel plBody;

	public PLSearch plSearch;
	public PLCharts plCharts;

//	public static String baseURL = "http://localhost:8080/";

	public static String baseURL = "http://cndata4u.appspot.com/";

	public InlineLabel loading;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		singleton = this;

		this.constants = GWT.create(CNStatsFrontConstants.class);
		loading = new InlineLabel(this.constants.loading());
		loading.setStyleName("appLoading");
		RootPanel.get().add(loading);
		RootPanel.get().setWidgetPosition(loading,
				(Window.getClientWidth()-loading.getOffsetWidth())/2, 0);
		this.showLoading(true);
		AjaxLoader.loadVisualizationApi(new VisLoaded(), AreaChart.PACKAGE,
				BarChart.PACKAGE, ColumnChart.PACKAGE, LineChart.PACKAGE,
				PieChart.PACKAGE, Table.PACKAGE);
	}

	public static CNStatsFront get() {
		return singleton;
	}
	
	public void showLoading(Boolean show) {
		loading.setVisible(show);
	}
}

final class VisLoaded implements Runnable {
	public void run() {
		// Continue to init
		Window.setTitle(CNStatsFront.get().constants.appName());

		CNStatsFront.get().plMain = new VerticalPanel();
		// CNStatsFront.get().ftHeader = new FlexTable();

		CNStatsFront.get().plBody = new DeckPanel();
		CNStatsFront.get().plSearch = new PLSearch();
		CNStatsFront.get().plCharts = new PLCharts();

		CNStatsFront.get().plMain.setSize("100%", "100%");
		// CNStatsFront.get().plMain.add(CNStatsFront.get().ftHeader);
		CNStatsFront.get().plMain.add(CNStatsFront.get().plBody);
		
		InlineLabel version = new InlineLabel("v1.0beta_b20090409");
		CNStatsFront.get().plMain.add(version);
		CNStatsFront.get().plMain.setCellHorizontalAlignment(version, HasHorizontalAlignment.ALIGN_RIGHT);
		CNStatsFront.get().plMain.setCellVerticalAlignment(version, HasVerticalAlignment.ALIGN_BOTTOM);

		CNStatsFront.get().plBody.setSize("100%", "100%");
		CNStatsFront.get().plBody.setAnimationEnabled(true);

		RootPanel.get().add(CNStatsFront.get().plMain);

		Window.addResizeHandler(new ResizeHandler() {
			public void onResize(ResizeEvent event) {
				CNStatsFront.get().plMain.setSize("100%", "100%");
				CNStatsFront.get().plBody.setSize("100%", "100%");
				RootPanel.get().setWidgetPosition(CNStatsFront.get().loading,
						(Window.getClientWidth()-CNStatsFront.get().loading.getOffsetWidth())/2, 0);
			}
		});
		CNStatsFront.get().showLoading(false);
		CNStatsFront.get().plSearch.show();
	}
}
